package com.lwm.controller;

import com.github.pagehelper.PageInfo;
import com.lwm.domain.MaterialCategory;
import com.lwm.domain.ResponseResult;
import com.lwm.service.MaterialCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 物料分类控制层
 */
@RestController
@RequestMapping("/materialCategory")
@Slf4j
public class MaterialCategoryController {
    @Resource
    private MaterialCategoryService materialCategoryService;

    /**
     * 新增物料分类
     *
     * @param materialCategory 物料分类
     * @return {@link ResponseResult}<{@link Object}>
     */
    @PostMapping
    public ResponseResult<Object> save(@RequestBody MaterialCategory materialCategory) {
        log.info("materialCategory {}", materialCategory);
        return materialCategoryService.save(materialCategory);
    }

    /**
     * 删除物料分类
     *
     * @param ids id组
     * @return {@link ResponseResult}<{@link Object}>
     */
    @DeleteMapping
    public ResponseResult<Object> remove(@RequestBody List<Long> ids) {
        log.info("ids {}", ids);
        return materialCategoryService.remove(ids);
    }

    /**
     * 编辑
     *
     * @param materialCategory 物料分类
     * @return {@link ResponseResult}<{@link Object}>
     */
    @PutMapping
    public ResponseResult<Object> edit(@RequestBody MaterialCategory materialCategory) {
        return materialCategoryService.edit(materialCategory);
    }

    /**
     * 编辑批处理
     *
     * @param list 物料分类集合
     * @return {@link ResponseResult}<{@link Object}>
     */
    @PutMapping("/editBatch")
    public ResponseResult<Object> editBatch(@RequestBody List<MaterialCategory> list) {
        log.info("批处理 list {}", list);
        return materialCategoryService.editBatch(list);
    }

    /**
     * 分页
     *
     * @param page             当前页码
     * @param size             数量
     * @param materialCategory 物料分类
     * @return {@link ResponseResult}<{@link PageInfo}<{@link MaterialCategory}>>
     */
    @GetMapping("/page")
    public ResponseResult<PageInfo<MaterialCategory>> page(Integer page, Integer size, MaterialCategory materialCategory) {
        log.info("当前页码: {}", page);
        log.info("数量: {}", size);
        log.info("物料分类数据 {}", materialCategory);

        return materialCategoryService.page(page, size, materialCategory);
    }

    /**
     * 根据id获取
     *
     * @param id 分类id
     * @return {@link ResponseResult}<{@link MaterialCategory}>
     */
    @GetMapping("/{id}")
    public ResponseResult<MaterialCategory> getById(@PathVariable Long id) {
        log.info("id {}", id);
        return materialCategoryService.getById(id);
    }

    /**
     * 获取对应id是否作为其他分类的父级而存在
     *
     * @param ids id集合
     * @return {@link ResponseResult}<{@link Object}>
     */
    @GetMapping("/getParentExist/{ids}")
    public ResponseResult<Object> getParentExist(@PathVariable List<Long> ids) {
        log.info("ids {}", ids);
        return materialCategoryService.getParentExist(ids);
    }
}
